//
//  ActivityRequest.swift
//  weile
//
//  Created by LKM on 16/9/27.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

class ActivityRequest {
    
    /**
     包了一层导航栏
     */
    class func presentNavBarWarp(_ parentVc:UIViewController,_ vc:UIViewController,_ hasBackBtn:Bool){
         let Navc = BaseNavigationController()
        Navc.addChildViewController(vc)
        Navc.addBackBtnToNavBar(vc)
        
        //丫的 不就是要一个导航栏么 哥给你
        parentVc.present(Navc, animated: true, completion: nil);
    }
    
    class func showShardDialogViewController(_ viewController: UIViewController ,title:String?,content:String?,url:String?,imageUrl:String?,localImage:UIImage?){
        
        
        let av = Bundle.main.loadNibNamed("ShardDialogViewController", owner: nil, options: nil)!.last as! ShardDialogViewController
            //ShardDialogViewController(title: "", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
//        av.modalPresentationStyle = UIModalPresentationStyle.currentContext
        
//        av.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
//        viewController.definesPresentationContext=true
        
        av.binData(title: title, content: content, url: url, imageUrl: imageUrl, localImage: localImage)
        viewController.present(av, animated: false, completion: nil)
    }

   
    class func goWebBrowseViewController(url:String,title:String){//,_ mUIViewController:UIViewController){
     
        let vc = MyWebBrowseViewController();
        vc.url=url;
        
        vc.title=title;
      
        presentNavBarWarp(AppRootViewController()!,vc,true);
        
//        if let un = mUIViewController.navigationController{
//            un.pushViewController(cv, animated: true)
//        }else if(mUIViewController is UINavigationController){
//            (mUIViewController as! UINavigationController).pushViewController(cv, animated: true)
//        }else{
//            mUIViewController.present(cv, animated: true, completion: nil);
//        }
        
    }
    class func AppRootViewController() -> UIViewController? {
//        var topVC = UIApplication.shared.keyWindow?.rootViewController
//        while topVC?.presentedViewController != nil {
//            topVC = topVC?.presentedViewController
//        }
//        return topVC
        
        var result:UIViewController?
        
        var window:UIWindow? = UIApplication.shared.keyWindow
        if (window?.windowLevel != UIWindowLevelNormal) {
            let windows = UIApplication.shared.windows
            for tmpWin in windows{
                if (tmpWin.windowLevel == UIWindowLevelNormal){
                    window = tmpWin;
                    break;
                }
            
            }
        }
        
        let frontView:UIView? = window?.subviews[0];
        let nextResponder:UIResponder?=frontView?.next;
        
        if (nextResponder?.isKind(of: UIViewController.classForCoder() )) ?? false {
            result=nextResponder as? UIViewController
        }else{
            result = window?.rootViewController;
        }
        return result;
    }
    
    
         
    
    class func getTipsViewController(_ tips:String?)-> TipsViewController{
        let cv = Bundle.main.loadNibNamed("TipsViewController", owner: nil, options: nil)!.last as! TipsViewController
        if( tips != nil ){
            cv.lbl_tips.setText(str: tips)
        }
        return cv
    }
    class func getSearchNewsViewController()-> SearchNewsViewController{
        
        let cv = Bundle.main.loadNibNamed("SearchNewsViewController", owner: nil, options: nil)!.last as! SearchNewsViewController
//        cv.automaticallyAdjustsScrollViewInsets=false;
        return cv
    }

    class func goSearchViewController(_ viewController:UINavigationController ,keyWord:String? = nil){
        
        viewController.pushViewController(getSearchViewController(keyWord:keyWord), animated: true)
        
        
    }

    class func getSearchViewController(keyWord:String? = nil,productsMainId:String? = nil) -> SearchViewController{
        let cv = Bundle.main.loadNibNamed("SearchViewController", owner: nil, options: nil)!.last as! SearchViewController
//        cv.automaticallyAdjustsScrollViewInsets=false;
//        cv.title="我的收藏"
        cv.productsMainId=productsMainId
        
        if keyWord != nil {
            //保证在viewController.pushViewController(cv, animated: true)后viewDidLoad 执行
            Handler.post {
                cv.searchForKeyWord(keyWord: keyWord!, isForceRefresh: true)
            }
            
        }
        return cv;
    }
    
    class func getMainBViewController()-> MainBViewController{
        let cv = Bundle.main.loadNibNamed("MainBViewController", owner: nil, options: nil)!.last as! MainBViewController
        return cv
    }
    class func getHelperViewController()-> HelperViewController{
        let cv = Bundle.main.loadNibNamed("HelperViewController", owner: nil, options: nil)!.last as! HelperViewController
        return cv
    }
    class func getHomeViewController()-> HomeViewController{
        let cv = Bundle.main.loadNibNamed("HomeViewController", owner: nil, options: nil)!.last as! HomeViewController
        return cv
        //
    }
    class func goMyCollectsViewController(_ viewController:UINavigationController ){
        let cv =  MyCollectsTabViewController()
        cv.automaticallyAdjustsScrollViewInsets=false;
        cv.title="我的收藏"
        viewController.pushViewController(cv, animated: true)
    }
    class func getMyCollectsProductViewController() -> MyCollectsProductViewController{
        let cv = Bundle.main.loadNibNamed("MyCollectsProductViewController", owner: nil, options: nil)!.last as! MyCollectsProductViewController
        cv.automaticallyAdjustsScrollViewInsets=false;
        cv.title="产品"
        return cv;
    }
    class func getMyCollectsArticleViewController() -> MyCollectsArticleViewController{
        let cv = MyCollectsArticleViewController()//Bundle.main.loadNibNamed("MyCollectsArticleViewController", owner: nil, options: nil)!.last as! MyCollectsArticleViewController
        cv.automaticallyAdjustsScrollViewInsets=false;
        cv.title="资讯"
        return cv;
    }
    
    
    class func goMyInfoViewController(_ viewController:UINavigationController ){
        let cv = Bundle.main.loadNibNamed("MyInfoViewController", owner: nil, options: nil)!.last as! MyInfoViewController
        cv.automaticallyAdjustsScrollViewInsets=false;
        cv.title="我的资料"
        viewController.pushViewController(cv, animated: true)
    }
    class func goFeedBackViewController(_ viewController:UINavigationController ){
        let cv = Bundle.main.loadNibNamed("FeedBackViewController", owner: nil, options: nil)!.last as! FeedBackViewController
        cv.title="反馈"
        viewController.pushViewController(cv, animated: true)
    }
    class func goHelpsViewController(_ viewController:UINavigationController ){
        let cv = HelpsViewController()
        cv.automaticallyAdjustsScrollViewInsets=false;//必须
        cv.title="帮助中心"
        viewController.pushViewController(cv, animated: true)
    }
    class func goHelpDetailController(_ viewController:UINavigationController,_ id:String){
        let cv = HelpDetailViewController();
        cv.title = R.string.name;
        cv.id=id
        viewController.pushViewController(cv, animated: true)
        
    }
    
    class func goContactServiceViewController(_ viewController:UINavigationController ){
        let cv = Bundle.main.loadNibNamed("ContactServiceViewController", owner: nil, options: nil)!.last as! ContactServiceViewController
        cv.title="联系客服"
        viewController.pushViewController(cv, animated: true)
    }
    

//    PassFindViewController

    class func getMyCenterViewController()-> MyCenterViewController{
        let cv = Bundle.main.loadNibNamed("MyCenterViewController", owner: nil, options: nil)![1] as! MyCenterViewController
//        iOS7之后，当UIViewController成为UINavigationController的控制视图,UIViewController的控制视图上的第一个子视图是UIScrollView，UIScrollView会往下偏移64px的位置，64px是UINavigationBar的44px高度加上状态条20px的高度。
//        
//        最简单的解决方法是设置UIViewController的automaticallyAdjustsScrollViewInsets属性为NO。
        cv.automaticallyAdjustsScrollViewInsets=false;
        return cv
//        return MyCenterViewController();
    }
    
    
    
    class func goArticleDetailController(_ viewController:UINavigationController ,_ aid:String){
       
        viewController.pushViewController(getArticleDetailController(aid), animated: true)
    }
    class func getArticleDetailController(_ aid:String)->ArticleDetailViewController{
        let cv = ArticleDetailViewController();
        cv.title = R.string.name;
        cv.articleId=aid
        return cv;
       
    }
    class func getArticlesViewController()-> ArticlesViewController{
        
        let cv = Bundle.main.loadNibNamed("ArticlesViewController", owner: nil, options: nil)!.last as! ArticlesViewController
        cv.automaticallyAdjustsScrollViewInsets=false;
        
        return cv
    }
    class func getSuppliersViewController()-> SuppliersViewController{
        
        let cv = Bundle.main.loadNibNamed("SuppliersViewController", owner: nil, options: nil)!.last as! SuppliersViewController
        cv.automaticallyAdjustsScrollViewInsets=false;
        
        return cv
    }
    

    class func goProductDetailController(_ viewController:UINavigationController,_ pid:String ){
        
        viewController.pushViewController(getProductDetailController(pid), animated: true)
    }
    
    class func getProductDetailController(_ pid:String ) -> ProductDetailViewController{
        let cv = ProductDetailViewController()
        //                detailVC.homeItem = items[(indexPath as NSIndexPath).row]
        cv.pid=pid;
        cv.title = R.string.name;
//        viewController.pushViewController(cv, animated: true)
        return cv;
    }
    class func getProductsViewController(mainId:String?)-> ProductsViewController{
        let cv = Bundle.main.loadNibNamed("ProductsViewController", owner: nil, options: nil)!.last as! ProductsViewController
        cv.automaticallyAdjustsScrollViewInsets=false;
        if mainId != nil{
            cv.mainId=mainId!;
        }
        return cv
    }
    
    class func goPassFind(_ viewController:UINavigationController){
        let cv = Bundle.main.loadNibNamed("Register", owner: nil, options: nil)!.last as! RegisterViewController
        cv.title="找回密码"
        
        cv.isFindPassPage=true
//        viewController.present(cv, animated: false, completion:nil)
        LKMLog("======cv.isFindPassPage=\(cv.isFindPassPage)")
        viewController.pushViewController(cv, animated: true)
    }
    class func goRegister(_ viewController:UINavigationController){
        let cv = Bundle.main.loadNibNamed("Register", owner: nil, options: nil)!.last as! RegisterViewController
        cv.title="注册"
//        viewController.present(cv, animated: true, completion:nil)//这样就没导航栏的
        viewController.pushViewController(cv, animated: true)
    }
    
    //会注销用户
    class func goLogin(){
        //去到登录 就应该是注销用户
        UserInfoCache.getInstance().loginOut();
        
        let cv = Bundle.main.loadNibNamed("login", owner: nil, options: nil)!.last as! LoginViewController
        cv.title="登录"
        
        let nav = BaseNavigationController()
        nav.addChildViewController(cv)

        AppDelegate.getInstance().window!.rootViewController=nav;
            
//        viewController.present(cv, animated: false, completion:nil)
    }

//    class func goMain(_ viewController:UIViewController){
//        let storyboard:UIStoryboard=UIStoryboard(name: "Main", bundle: nil)
//        
//        //self.presentViewController(storyboard.instantiateInitialViewController() as UIViewController, animated: true, completion:nil)
//        //“main”就是storyboard ID
//        viewController.present(storyboard.instantiateViewController(withIdentifier: "main") as UIViewController, animated: true, completion:nil)
//
//    }
    
    class func goUserProtocolViewController(_ viewController:UINavigationController){
        let cv = UserProtocolViewController();
        cv.title = R.string.name;
        viewController.pushViewController(cv, animated: true)
        
    }

    class func goMain(){
        AppDelegate.getInstance().window!.rootViewController = getMainBViewController()// MainViewController();
    }
    
    /**
     selectedIndex 跳转到第几
     */
    class func goMainTab(_ viewController:UIViewController, selectedIndex :Int,subSelectIndex:Int = 0){
        //一定要在MainTabViewController() 之前 因为MainTabViewController()执行玩之后 HomeViewController的视图已经准备好了
        if(selectedIndex == 0 ){
            //产品的时候跳转
            HomeViewController.SelectIndex=subSelectIndex;
        }
        
        let cv = MainTabViewController();
        AppDelegate.getInstance().window!.rootViewController = cv//MainTabViewController();//因为根本无法返回，直接替换咯
        
        cv.selectedIndex=selectedIndex;//tabBar.select
        
        
        
//        viewController.present(MainTabViewController(), animated: true, completion: nil)
    }
    
    
    class func goInitHold(_ window: UIWindow){
        let cv = Bundle.main.loadNibNamed("InitHoldViewController", owner: nil, options: nil)!.last as!InitHoldViewController
        //let cv:InitHoldViewController = InitHoldViewController (nibName: "InitHoldViewController", bundle:nil) //这个会报错
        window.rootViewController=cv;
    }
    
}
